# -*- coding: UTF-8 -*-


from .formview import FormView
import copy


class X2mBase(object):
    def __init__(self, field_info, env=None, type=None, parent=None, **kwargs):
        self.env = env
        self._type = type
        self.field_info = field_info
        self.parent_info = parent

    @property
    def parent(self):
        info = self.parent_info
        action = info['action']
        view = info['view']
        fields = view['fields']
        # print('parent', action)
        # print('parent view',   view)
        # print('parent view fields',   fields)
        return FormView(action, env=self.env, fields=fields)

    @property
    def fields(self):
        views = self.field_info['views']
        view = views.get(self._type, {})
        fields = view.get('fields', {})
        return fields

    @property
    def fields_all(self):
        views = self.field_info['views']
        formview = views.get('form', {})
        treeview = views.get('tree', {})
        fields_form = formview.get('fields', {})
        fields_tree = treeview.get('fields', {})
        fields = {}
        fields.update(fields_form)
        fields.update(fields_tree)
        return fields

    @property
    def fields_list(self):
        return list(self.fields.keys())

    @property
    def Model(self):
        model = self.field_info['relation']
        fields = self.fields_all
        Model = self.env.model(model, fields=fields)
        return Model
